﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using DataAccess;
using Entities;
using Common;

namespace BusinessLogic
{
    public static class NhaCungCap
    {
        public static Entities.NhaCungCap[] SelectNhaCungCap(Entities.NhaCungCap cungcap)
        {
            Entities.NhaCungCap[] list = null;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.NhaCungCap.sp_Select_NhaCungCap, cn);
                cmd.Parameters.Add(Common.Constants.NhaCungCap.HanhDong, SqlDbType.NVarChar).Value = cungcap.HanhDong;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.MaNhaCungCap, SqlDbType.VarChar).Value = cungcap.MaNhaCungCap;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                string directory = System.Windows.Forms.Application.StartupPath.ToString() + @"\SuppliersImage";
                Common.Utilities.CreateDirectory(directory);
                while (dr.Read())
                {
                    Entities.NhaCungCap row = new Entities.NhaCungCap();
                    row.MaNhaCungCap = dr[Common.Constants.NhaCungCap.MaNhaCungCap].ToString();
                    row.TenNhaCungCap = dr[Common.Constants.NhaCungCap.TenNhaCungCap].ToString();
                    row.NoHienThoi = dr[Common.Constants.NhaCungCap.NoHienThoi].ToString();
                    row.DiaChi = dr[Common.Constants.NhaCungCap.DiaChi].ToString();
                    row.DienThoai = dr[Common.Constants.NhaCungCap.DienThoai].ToString();
                    row.Email = dr[Common.Constants.NhaCungCap.Email].ToString();
                    row.Website = dr[Common.Constants.NhaCungCap.Website].ToString();
                    row.Fax = dr[Common.Constants.NhaCungCap.Fax].ToString();
                    row.NguoiLienHe = dr[Common.Constants.NhaCungCap.NguoiLienHe].ToString();
                    row.MaSoThue = dr[Common.Constants.NhaCungCap.MaSoThue].ToString();
                    string links = System.Windows.Forms.Application.StartupPath.ToString() + @"\SuppliersImage\" + dr[Common.Constants.NhanVien.LinkImage].ToString();
                    row.LinkImage = dr[Common.Constants.NhaCungCap.LinkImage].ToString();
                    row.LinkImageRoot = links;
                    row.Image = null;
                    row.GhiChu = dr[Common.Constants.NhaCungCap.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.NhaCungCap.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.NhaCungCap[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.NhaCungCap)arr[i];
                    }
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;
        }

        public static int InsertUpdate(Entities.NhaCungCap cungcap)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.NhaCungCap.sp_InsertUpdate_NhaCungCap, cn);
                cmd.Parameters.Add(Common.Constants.NhaCungCap.HanhDong, SqlDbType.NVarChar).Value = cungcap.HanhDong;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.MaNhaCungCap, SqlDbType.VarChar).Value = cungcap.MaNhaCungCap;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.TenNhaCungCap, SqlDbType.NVarChar).Value = cungcap.TenNhaCungCap;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.NoHienThoi, SqlDbType.Float).Value = cungcap.NoHienThoi;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.DiaChi, SqlDbType.NVarChar).Value = cungcap.DiaChi;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.DienThoai, SqlDbType.NVarChar).Value = cungcap.DienThoai;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.Email, SqlDbType.NVarChar).Value = cungcap.Email;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.Website, SqlDbType.NVarChar).Value = cungcap.Website;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.Fax, SqlDbType.NVarChar).Value = cungcap.Fax;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.NguoiLienHe, SqlDbType.NVarChar).Value = cungcap.NguoiLienHe;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.MaSoThue, SqlDbType.NVarChar).Value = cungcap.MaSoThue;
                string linked = string.Empty;
                if (cungcap.LinkImage != string.Empty && cungcap.LinkImage != null)
                {
                    linked = cungcap.MaNhaCungCap + ".jpeg";
                }
                else
                {
                    linked = string.Empty;
                }
                cmd.Parameters.Add(Common.Constants.NhaCungCap.LinkImage, SqlDbType.NVarChar).Value = linked;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.GhiChu, SqlDbType.NVarChar).Value = cungcap.GhiChu;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.Deleted, SqlDbType.Bit).Value = cungcap.Deleted;
                rt = cmd.ExecuteNonQuery();
                if (rt != 0)
                {
                    try
                    {
                        if (linked != string.Empty)
                        {
                            string links = @"\SuppliersImage\" + linked;
                            string linksRoot = System.Windows.Forms.Application.StartupPath.ToString() + links;
                            try { Common.Utilities.DeleteFile(links); }
                            catch { }
                            System.IO.MemoryStream ms = new System.IO.MemoryStream(cungcap.Image);
                            Bitmap bmp = new Bitmap(ms);
                            try
                            {
                                bmp.Save(linksRoot, System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                            catch { }
                        }
                    }
                    catch (Exception){}
                    try
                    {
                        int k = BusinessLogic.ChiTietHangHoaNhaCungCap.InsertUpdateList(cungcap.DanhSachHangHoa);
                    }
                    catch (Exception) {}
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
            }
            return rt;
        }

        public static byte[] GetImage(string linksRoot)
        {
            byte[] image = null;
            try
            {
                image = Common.Utilities.GetImage(linksRoot);
            }
            catch (Exception ex)
            { string s = ex.Message; image = null; }
            return image;
        }

        public static int Delete(Entities.NhaCungCap cungcap)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.NhaCungCap.sp_Deleted_NhaCungCap, cn);
                cmd.Parameters.Add(Common.Constants.NhaCungCap.HanhDong, SqlDbType.NVarChar).Value = cungcap.HanhDong;
                cmd.Parameters.Add(Common.Constants.NhaCungCap.MaNhaCungCap, SqlDbType.VarChar).Value = cungcap.MaNhaCungCap;
                rt = cmd.ExecuteNonQuery();
                if (rt != 0)
                {
                    //truyen vao ten file anh
                    Common.Utilities.DeleteFile(cungcap.LinkImage);
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
            }
            return rt;
        }


        /// <summary>
        /// loivt
        /// </summary>
        /// <returns></returns>
        public static Entities.NhaCungCap[] Select()
        {
            Entities.NhaCungCap[] list = null;
            try
            {

                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.NhaCungCap.sp_Select_NCC, cn);
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                string directory = System.Windows.Forms.Application.StartupPath.ToString() + @"\NhaCungCapImage";
                Common.Utilities.CreateDirectory(directory);
                while (dr.Read())
                {
                    Entities.NhaCungCap row = new Entities.NhaCungCap();
                    row.MaNhaCungCap = dr[Common.Constants.NhaCungCap.MaNhaCungCap].ToString();
                    row.TenNhaCungCap = dr[Common.Constants.NhaCungCap.TenNhaCungCap].ToString();
                    row.NoHienThoi = dr[Common.Constants.NhaCungCap.NoHienThoi].ToString();
                    row.DiaChi = dr[Common.Constants.NhaCungCap.DiaChi].ToString();
                    row.DienThoai = dr[Common.Constants.NhaCungCap.DienThoai].ToString();
                    row.Email = dr[Common.Constants.NhaCungCap.Email].ToString();
                    row.Website = dr[Common.Constants.NhaCungCap.Website].ToString();
                    row.Fax = dr[Common.Constants.NhaCungCap.Fax].ToString();
                    row.NguoiLienHe = dr[Common.Constants.NhaCungCap.NguoiLienHe].ToString();
                    row.MaSoThue = dr[Common.Constants.NhaCungCap.MaSoThue].ToString();
                    string links = System.Windows.Forms.Application.StartupPath.ToString() + @"\SuppliersImage\" + dr[Common.Constants.NhanVien.LinkImage].ToString();
                    row.LinkImage = dr[Common.Constants.NhaCungCap.LinkImage].ToString();
                    row.LinkImageRoot = links;
                    row.Image = null;
                    row.GhiChu = dr[Common.Constants.NhaCungCap.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.NhaCungCap.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.NhaCungCap[0];
                }
                else
                {
                    list = new Entities.NhaCungCap[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.NhaCungCap)arr[i];
                    }
                }
                cmd.Connection.Dispose();
                cn.Close();
                return list;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.NhaCungCap[0]; }
           

        }

        // Selct nhà cung cấp
        public static Entities.NhaCungCap SelectMaNCC(Entities.NhaCungCap nhacungcap1)
        {
            Entities.NhaCungCap nhacungcap = new Entities.NhaCungCap();
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.NhaCungCap.sp_Select_NhaCungCap, cn);
                cmd.Parameters.Add(Common.Constants.NhaCungCap.HanhDong, SqlDbType.NVarChar).Value = "3";
                cmd.Parameters.Add(Common.Constants.NhaCungCap.MaNhaCungCap, SqlDbType.NVarChar, 50).Value = nhacungcap1.MaNhaCungCap;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    nhacungcap.TenNhaCungCap = dr[Common.Constants.NhaCungCap.TenNhaCungCap].ToString();
                    nhacungcap.DiaChi = dr[Common.Constants.NhaCungCap.DiaChi].ToString();
                    nhacungcap.DienThoai = dr[Common.Constants.NhaCungCap.DienThoai].ToString();
                    nhacungcap.Email = dr[Common.Constants.NhaCungCap.Email].ToString();
                    nhacungcap.Website = dr[Common.Constants.NhaCungCap.Website].ToString();
                    nhacungcap.Fax = dr[Common.Constants.NhaCungCap.Fax].ToString();
                    nhacungcap.NguoiLienHe = dr[Common.Constants.NhaCungCap.NguoiLienHe].ToString();
                    nhacungcap.MaSoThue = dr[Common.Constants.NhaCungCap.MaSoThue].ToString();
                    nhacungcap.DuNo = dr[Common.Constants.NhaCungCap.NoHienThoi].ToString();
                    nhacungcap.GhiChu = dr[Common.Constants.NhaCungCap.GhiChu].ToString();
                    nhacungcap.Deleted = bool.Parse(dr[Common.Constants.NhaCungCap.Deleted].ToString());
                    nhacungcap.MaNhaCungCap = dr[Common.Constants.NhaCungCap.MaNhaCungCap].ToString();
                    if (dr[Common.Constants.NhaCungCap.LinkImage].ToString() != "")
                    {
                        try
                        {
                            nhacungcap.LinkImage = System.Windows.Forms.Application.StartupPath.ToString() + @"\NhaCungCapImage\" + dr[Common.Constants.NhaCungCap.LinkImage].ToString() + ".jpg";
                            nhacungcap.Image = XuLyAnh(nhacungcap.LinkImage);
                        }
                        catch
                        {
                        }
                    }
                    else
                    {
                        nhacungcap.LinkImage = null;
                        nhacungcap.Image = null;
                    }
                }

                return nhacungcap;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.NhaCungCap(); }
            finally
            {
            }
        }

        // xử lý ảnh
        private static byte[] XuLyAnh(string links)
        {
            byte[] image = null;
            try
            {
                Bitmap bmp = new Bitmap(links);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] manganh = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(manganh, 0, Convert.ToInt32(ms.Length));
                image = manganh;

                ms.Dispose();
                ms.Close();
                ms.Flush();
                bmp.Dispose();
                bmp.Clone();
            }
            catch (Exception ex)
            { string s = ex.Message; }
            return image;
        }

    }
}

